package com.ctl.test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StudentController extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 3351894638847874589L;

    public StudentController() {
        super();
    }

    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String action = request.getParameter("action");
        String stuName = request.getParameter("stuName");
        String stuSex = request.getParameter("stuSex");
        String stuNumber = request.getParameter("stuNumber");
        int stuId = Integer.parseInt(request.getParameter("stuId"));
        SelectMethod sm = new SelectMethod();
        //新增
        if (action.equals("save")) {
            sm.insertStudentInfo(stuName, stuSex, stuNumber);
            response.sendRedirect("success.jsp");
        }
        //更新
        if (action.equals("update")) {
            Student stu = new Student();
            stu.setStuId(stuId);
            stu.setStuName(stuName);
            stu.setStuNumber(stuNumber);
            stu.setStuSex(stuSex);
            sm.updateStudentInfo(stu);
            response.sendRedirect("success.jsp");
        }
        //新增跳转
        if(action.equals("add")){
            request.getSession().setAttribute("stuId", stuId);
            response.sendRedirect("add.jsp");
        }
        //更新跳转
        if (action.equals("target")) {
            Student student = sm.getStuInfoById(stuId);
            request.getSession().setAttribute("stu", student);
            response.sendRedirect("update.jsp");
        }
        //删除
        if (action.equals("delete")) {
            System.out.println(sm.deleteStudentInfo(stuId) + "条数据受影响!");
            response.sendRedirect("success.jsp");
        }
    }

    public void init() throws ServletException {
        // Put your code here
    }
}
